package com.itheima.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.itheima.dao.OrderDao;
import com.itheima.dao.ProductDao;
import com.itheima.domain.Order;
import com.itheima.domain.Product;
import com.itheima.service.ProductService;
import com.itheima.utils.MybatisUtils;
import org.apache.ibatis.session.SqlSession;

import java.sql.Timestamp;
import java.util.List;
import java.util.UUID;

/**
 * @Package: com.itheima.service.impl
 * @ClassName: ProductServiceImpl
 * @Author: 王林磊
 * @CreateTime: 2021/9/27 20:21
 * @Description:
 */
public class ProductServiceImpl implements ProductService {
    @Override
    public void save(Product product) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        String id = UUID.randomUUID().toString();
        product.setId(id);
        mapper.save(product);
        sqlSession.close();
    }

    @Override
    public void delete(String[] ids) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        mapper.delete(ids);
        sqlSession.close();
    }

    @Override
    public void update(Product product) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        mapper.update(product);
        sqlSession.close();
    }

    @Override
    public Product findById(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        Product byId = mapper.findById(id);
        sqlSession.close();
        return byId;
    }

 /*   @Override
    public List<Product> findAll(Product product) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        List<Product> all = mapper.findAll(product);
        return all;
    }*/

    @Override
    public PageInfo findAll(Product product) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        int size = product.getPageSize();
        int page = product.getPageNum();
        PageHelper.startPage(page, size);
        List<Product> all = mapper.findAll(product);
        PageInfo pageInfo = new PageInfo(all);
        sqlSession.close();
        return pageInfo;
    }

    @Override
    public List<String> findCity() {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        List<String> city = mapper.findCity();
        sqlSession.close();
        return city;
    }

    @Override
    public void updateStatus(String id) {
        SqlSession sqlSession = MybatisUtils.getSqlSession();
        ProductDao mapper = sqlSession.getMapper(ProductDao.class);
        Product product = mapper.findById(id);
        int productStatus = product.getProductStatus() == 1 ? 0 : 1;
        mapper.updateStatus(id,productStatus);
        sqlSession.close();
    }
}
